//
//  UIImage+LxImage.m
//  LvXiMark
//
//  Created by charlicar on 16/5/12.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "UIImage+LxImage.h"

@implementation UIImage (LxImage)

+ (instancetype)imageWithOriginalImageName:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}


+(instancetype)imageWithColor:(UIColor *)color size:(CGSize)size{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;

}

+ (instancetype)imageWithMargin:(CGFloat)margin marginColor:(UIColor *)color image:(UIImage *)image
{
    CGSize size = CGSizeMake(margin * 2 + image.size.width , margin * 2 + image.size.height);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    //设置一个大圆作为边框
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
    [color set];
    [path fill];
    //添加一个小圆
    path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(margin, margin, image.size.width, image.size.height)];
    
    [path addClip];
    [image drawInRect:CGRectMake(margin, margin, image.size.width, image.size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
    
}


-(UIImage*)scaleToSize:(CGSize)size{
    UIGraphicsBeginImageContext(size);  //size 为CGSize类型，即你所需要的图片尺寸
    
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return scaledImage;   //返回的就是已经改变的图片
}

+(UIImage *)blurScreenShot:(CGFloat)blurRadius view:(UIView *)view{
    
    UIView * superView = view.superview;
    if(superView==nil)return nil;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.width, view.height),NO,1);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CIFilter * blur = [CIFilter filterWithName:@"CIGaussianBlur"];
    if(blur == nil)return nil;
    [blur setValue:[[CIImage alloc] initWithImage:image] forKey:kCIOutputImageKey];
    [blur setValue:@(blurRadius) forKey:kCIInputRadiusKey];
    
    CIContext * ciContext = [CIContext contextWithOptions:nil];
    
    CIImage * result = [blur valueForKey:kCIOutputImageKey];
    
    CGRect  boundingRect = CGRectMake(0, 0, view.width, view.height);
    
    struct CGImage * cgImage = [ciContext createCGImage:result fromRect:boundingRect];
    
    UIImage * filteredImage = [UIImage imageWithCGImage:cgImage];
    return filteredImage;
}
@end
